<?php
/**
 * Created by JetBrains PhpStorm.
 * User: root
 * Date: 13-12-25
 * Time: 上午10:23
 * To change this template use File | Settings | File Templates.
 */
namespace Common\Org\Email;
use Think\Exception;

class Email {
    protected $config = array();

    //邮件内容
    protected $email = array();
    protected $them = '';
    protected $body = '';

    protected $error = '';

    /*private function __construct(){

    }*/

    /**
     * 取得邮件类实例
     * @static
     * @access public
     * @return mixed 返回邮件驱动类
     */
    public static function getInstance() {
        $args = func_get_args();
        static $obj;
        if(!$obj){
            $thisobj = new Email();
            $obj = call_user_func_array(array(&$thisobj, 'factory'), $args);
        }

        return $obj;
    }

    /**
     * 加载邮件类
     * @access public
     * @param string $type 邮件发送类型
     * @return string
     */
    public function factory($type='') {
        // 读取数据库配置
        $config = $this->parseConfig($type);
        if(empty($config))
            throw_exception(L('_NO_EMAIL_TYPE_'.$type));//未定义邮件类型

        // 邮件类型
        $this->emailType = ucwords(strtolower($config['type']));
        $class = 'Email'. $this->emailType;

//        require_once "Driver/{$class}.class.php";
        $class = "\\Common\\Org\\Email\\Driver\\{$class}";

        // 检查驱动类
        if(class_exists($class)) {
            $email = new $class($config);
            // 获取当前的邮件类型
            $email->emailType = strtoupper($this->emailType);
        } else {
            // 邮件驱动类没有定义
            throw_exception(L('_NO_EMAIL_DRIVER_').': ' . $class);
        }
        return $email;
    }

    /**
     * 配置处理
     */
    protected function parseConfig($type) {
        if($type==1){
//            $alias = 'service';
        } else if($type==2){
//            $alias = 'smtp';
        } else {
            throw new Exception('非法类型');
        }

        $alias = 'email';
        if(S('send_email_config_'.$alias)){
            $config = S('send_email_config_'.$alias);
        }else{
            $where = array();
            $where['alias'] = $alias;
            $where['del'] = 0;
            $where['status'] = 1;
            $config = M('email_config')->where($where)->field('url,account,password,send_email,remind_email')->find();
            S('send_email_config_'.$alias,$config);
            unset($where);
        }

        if(empty($config)){
            $key = strtoupper("SEND_EMAIL_{$type}");
            $config = C($key);
        }

        $config['type'] = 'smtp';
        $config['replier'] = SITE_NAME;
        $config['sender'] =  SITE_NAME;
        $config['reply_email'] = $config['send_email'];

        return $config;
    }

    /**
     * 设置邮箱
     */
    public function setEmail($email) {
        if(is_array($email)) {
            $this->email = array_merge($this->email, $email);
        } elseif(is_string($email) && strpos($email, ',')) {
            $this->email = array_merge($this->email, explode(',', $email));
        } elseif(is_string($email) && strpos($email, ';')) {
            $this->email = array_merge($this->email, explode(',', $email));
        } elseif(is_string($email)) {
            $this->email[] = $email;
        }
        return;
    }

    /**
     * 设置主题
     */
    public function setThem($them) {
        if(!empty($them)) {
            $this->them = $them;
        }
        return;
    }

    /**
     * 设置邮件主体
     */
    public function setBody($body) {
        if(!empty($body)) {
            $this->body = $body;
        }
        return;
    }

    /**
     * 发送邮件
     */
    public function sendEmail() {

        if(empty($this->email)) {
            $this->error = '未设置接收邮箱';
            return false;
        }

        if(empty($this->them)) {
            $this->error = '未设置邮件主题';
            return false;
        }

        if(empty($this->body)) {
            $this->error = '未设置邮件主体';
            return false;
        }
        $return = $this->send();
        $this->email = array();
        $this->them = '';
        $this->body = '';

        return $return;
    }

    public function getError() {
        return $this->error;
    }
}